var MongoClient = require('mongodb').MongoClient;

function dbClient(host, db) {
	if (host) {
		this._host = host;
	} else {

	}

	if (db) {
		this._db = db;
	} else {

	}

};

dbClient.prototype.insert = function insert(collection, info, callback) {
	console.log("insert " + this._host + this._db);
	MongoClient.connect(this._host + this._db, function(err1, db) {
		if (!err1) {
			console.log('db is connected!');
			this._collection = db.collection(collection);
			this._collection.insert(info, function(err2, result) {
				callback(err1, err2, result);
				db.close();
			});
		} else {
			console.log(err1);
		}
	});
};

dbClient.prototype.findOne = function(col, query, callback) {
	MongoClient.connect(this._host + this._db, function(err1, db) {
		if (!err1) {
			console.log('db is connected');
			this._collection = db.collection(col);
			this._collection.findOne(query, function(err2, doc) {
				if (!err2) {
					db.close();
					return callback(err1, err2, doc);
				} else {
					console.log(err2);
					db.close();
					return callback(err1, err2, null);
				}

			});
		} else
			console.log(err1);
	});

};
dbClient.prototype.find = function(col, query, callback) {
	MongoClient.connect(this._host + this._db, function(err1, db) {
		if (!err1) {
			console.log('db is connected');
			this._collection = db.collection(col);
			this._collection.find(query).toArray(function(err2, doc) {
				if (!err2) {
					db.close();
					return callback(err1,err2,doc);
				} else {
					console.log(err2);
					db.close();
					return callback(err1,err2, null);
				}

			});
		} else
			console.log(err1);
	});
};

dbClient.prototype.remove = function(col, selector, callback) {
	MongoClient.connect(this._host + this._db, function(err1, db) {
		if (!err1) {
			console.log('db is connected');
			this._collection = db.collection(col);
			this._collection.remove(selector,
					function(err2, numberOfRemoveDocs) {
						if (!err2) {
							db.close();
							return callback(err1,err2,numberOfRemoveDocs);
						} else {
							console.log(err2);
							db.close();
							return callback(err1,err2,null);
						}
					});
		} else
			console.log(err1);
	});
};

dbClient.prototype.update = function(col, selector, document, callback) {
	MongoClient.connect(this._host + this._db, function(err1, db) {
		var err3;
		if (!err1) {
			console.log('it is connected');
			this._collection = db.collection(col);
			this._collection.update(selector, {
				$set : document
			}, function(err, doc) {
				if (doc)
					console.log('update success');
				else {
					console.log('nothing found');
				}
				err3=err;
			});
			this._collection.findOne(selector, function(err2, doc) {
				db.close();
				return callback(err1,err2,err3, doc);
			});
		} else {
			console.log(err1);
			db.close();
			return callback(err1,err2,err3, null);

		}
	});
	// db.things.update({name:"mongo"},{$set:{name:"mongo_new"}});
}

module.exports = dbClient;